package com.example.deliveryapplication

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.deliveryapplication.entity.Plan
import com.example.deliveryapplication.entity.PlanItem
import com.example.deliveryapplication.entity.PlanWithPlanItem
import com.example.deliveryapplication.entity.SelectString

class PlanAdapter(private val context: Context, private val mList: List<PlanWithPlanItem>,private val listener:CallBack) :
    BaseRecyclerViewAdapter(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_plan_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            when (data.plan.type) {
                0 -> {
                    holder.billType.text = "草稿单"
                    holder.billType.setBackgroundResource(R.drawable.bg_solid_red_radius5dp)
                }
                else -> {
                    holder.billType.text = "完成单"
                    holder.billType.setBackgroundResource(R.drawable.bg_btn_green_radius)
                }
            }
            holder.clientNum.text=data.clientCount.handlerNull()
            holder.createTime.text=data.plan.insertTime
            holder.mParentLL.setOnClickListener {

            }
            holder.content.setOnClickListener {
                context.startActivity(Intent(context,NewPlanActivity::class.java).putExtra("planId",data.plan.id).putExtra("whereFrom",1))
            }
            holder.num.text=data.itemCount.handlerNull()
            holder.deleteTV.setOnClickListener {
                listener.onItemDelete(position,data)
            }
            holder.copyTV.setOnClickListener {
                listener.onItemCopy(position,data)
            }
            holder.saveTime.text=data.plan.time
        }
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val billType: TextView = view.findViewById(R.id.billType)
        val createTime: TextView = view.findViewById(R.id.createTime)
        val num: TextView = view.findViewById(R.id.num)
        val mParentLL:LinearLayout=view.findViewById(R.id.mParentLL)
        val content:LinearLayout=view.findViewById(R.id.content)
        val deleteTV:TextView=view.findViewById(R.id.deleteTV)
        val copyTV:TextView=view.findViewById(R.id.copyTV)
        val saveTime:TextView=view.findViewById(R.id.saveTime)
        val clientNum:TextView=view.findViewById(R.id.clientNum)
    }

    interface CallBack {
        fun onItemDelete(position: Int, data: PlanWithPlanItem)
        fun onItemCopy(position: Int, data: PlanWithPlanItem)
    }

}